package com.muyu.user.controller;

import java.util.List;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.user.domain.SysUserGroup;
import com.muyu.user.domain.req.SysUserGroupQueryReq;
import com.muyu.user.domain.model.SysUserGroupQueryModel;
import com.muyu.user.domain.model.SysUserGroupPageQueryModel;
import com.muyu.user.domain.resp.SysUserGroupPageResp;
import com.muyu.user.domain.req.SysUserGroupSaveReq;
import com.muyu.user.domain.req.SysUserGroupEditReq;
import com.muyu.user.domain.model.SysUserGroupSaveModel;
import com.muyu.user.domain.model.SysUserGroupEditModel;
import com.muyu.user.service.SysUserGroupService;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;

/**
 * 用户分组Controller
 *
 * @author liuguoqiang
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/group")
@Tag(name = "用户分组", description = "用户分组控制层")
public class SysUserGroupController extends BaseController {

    /**
    * 用户分组业务层
    */
    @Autowired
    private SysUserGroupService sysUserGroupService;

    /**
     * 查询用户分组列表
     */
    @Operation(summary = "获取用户分组列表", description = "获取用户分组列表")
    @RequiresPermissions("user:group:list")
    @GetMapping("/list")
    public Result<DataPageResp<SysUserGroupPageResp>> list(SysUserGroupQueryReq sysUserGroupQueryReq) {
        PageQueryModel<SysUserGroupPageQueryModel> pageQueryModel = sysUserGroupService.pageQuery(SysUserGroupQueryModel.reqBuild(sysUserGroupQueryReq));
        return Result.success(DataPageResp.of(pageQueryModel).cover(SysUserGroupPageResp::infoBuild));
    }

    /**
     * 导出用户分组列表
     */
    @Operation(summary = "导出用户分组列表", description = "导出用户分组列表")
    @RequiresPermissions("user:group:export")
    @Log(title = "用户分组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserGroup sysUserGroup) {
        List<SysUserGroup> list = sysUserGroupService.exportList(sysUserGroup);
        ExcelUtil<SysUserGroup> util = new ExcelUtil<>(SysUserGroup.class);
        util.exportExcel(response, list, "用户分组数据");
    }

    /**
     * 获取用户分组详细信息
     */
    @Operation(summary = "获取用户分组详细信息", description = "获取用户分组详细信息")
    @RequiresPermissions("user:group:query")
    @GetMapping(value = "/{groupId}")
    public Result<SysUserGroup> getInfo(@PathVariable("groupId") Long groupId) {
        return Result.success(sysUserGroupService.getById(groupId));
    }

    /**
     * 新增用户分组
     */
    @RequiresPermissions("user:group:add")
    @Log(title = "用户分组", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增用户分组", description = "新增用户分组")
    public Result<String> add(@RequestBody SysUserGroupSaveReq sysUserGroupSaveReq) {
        sysUserGroupService.save(SysUserGroupSaveModel.saveReqBuild(sysUserGroupSaveReq));
        return Result.success();
    }

    /**
     * 修改用户分组
     */
    @RequiresPermissions("user:group:edit")
    @Log(title = "用户分组", businessType = BusinessType.UPDATE)
    @PutMapping("/{groupId}")
    @Operation(summary = "修改用户分组", description = "修改用户分组")
    public Result<String> edit(@PathVariable(value = "groupId") Long groupId, @RequestBody SysUserGroupEditReq sysUserGroupEditReq) {
        sysUserGroupService.edit(SysUserGroupEditModel.editReqBuild(groupId,sysUserGroupEditReq));
        return Result.success();
    }

    /**
     * 删除用户分组
     */
    @RequiresPermissions("user:group:remove")
    @Log(title = "用户分组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    @Operation(summary = "删除用户分组", description = "删除用户分组")
    public Result<String> remove(@PathVariable(value = "groupIds") List<Long> groupIds) {
        return toAjax(sysUserGroupService.removeBatchByIds(groupIds));
    }
    /**
     * 获取用户分组下拉框选项
     */
    @Operation(summary = "获取用户分组下拉框选项", description = "获取用户分组下拉框选项")
    @GetMapping("/options")
    public Result<List<SysUserGroup>> getGroupOptions() {
        List<SysUserGroup> groups = sysUserGroupService.getGroupOptions();
        return Result.success(groups);
    }
}
